const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    baseUrl: '',
    topicList: [],
    videoList: [],
    doctors: [],
    myDoctorInfo: '',
    navFlag: 1, //1 视频 2 文章
    user: app.globalData.user,

  },

  toPage(e) {
    console.log(e);
    var flag = e.currentTarget.dataset.flag;
    if (flag == 'videoList') {
      var topicType = 1,
        doctorId = 0;
      wx.navigateTo({
        url: '/content/pages/videoList/videoList?topicType=' + topicType + '&doctorId=' + doctorId + '&doctors=' + JSON.stringify(this.data.doctors),
      })
    } else if (flag == 'healthRecords') {
      wx.navigateTo({
        url: '/user/pages/healthRecords/healthRecords',
      })
    } else if (flag == 'registration') {
      wx.navigateTo({
        url: '/hospital/pages/registration/registration',
      })
    } else if (flag == 'doctorDetail') {
      if (this.data.user.userType == 2) {
        return
      }
      var doctor = e.currentTarget.dataset.doctor
      console.log(doctor);
      if (doctor != '') {
        wx.navigateTo({
          url: '/doctor/pages/doctorDetail/doctorDetail?doctor=' + JSON.stringify(doctor),
        })
      } else {
        wx.showToast({
          title: '暂无绑定医生',
          icon: 'none'
        })
      }
    } else if (flag == 'keFu') {
      wx.showToast({
        title: '功能暂未开启',
        icon: 'none'
      })
    } else if (flag == 'doctorRegistration') {
      wx.navigateTo({
        url: '/hospital/pages/doctorRegistrationHistory/doctorRegistrationHistory',
      })
    } else if (flag == 'myDetail') {
      var doctor = app.globalData.doctor || JSON.parse(wx.getStorageSync('doctor'))
      wx.navigateTo({
        url: '/doctor/pages/doctorDetail/doctorDetail?doctor=' + JSON.stringify(doctor),
      })
    } else if (flag == 'more') {

      var topicType = this.data.navFlag == 1 ? 1 : 0,
        doctorId = app.globalData.userType == 2 ? app.globalData.doctorId : 0

      wx.navigateTo({
        url: '/content/pages/videoList/videoList?topicType=' + topicType + '&doctorId=' + doctorId + '&doctors=' + JSON.stringify(this.data.doctors),
      })
    } else if (flag == 'doctorList') {
      wx.navigateTo({
        url: '/hospital/pages/doctorList/doctorList',
      })
    } else if (flag == 'zhiBo') {
      wx.showToast({
        title: '功能暂未开启',
        icon: 'none'
      })
    } else if (flag == 'yao') {
      wx.showToast({
        title: '功能暂未开启',
        icon: 'none'
      })
    } else if (flag == 'nav') {
      wx.showToast({
        title: '功能暂未开启',
        icon: 'none'
      })
    } else if (flag == 'watchZhiBo') {
      wx.showToast({
        title: '功能暂未开启',
        icon: 'none'
      })
    }
  },

  selectNav(e) {
    var flag = e.currentTarget.dataset.flag;
    this.setData({
      navFlag: flag
    })
  },

  toVideoDetail(e) {
    var obj = e.currentTarget.dataset.obj,
      flag = e.currentTarget.dataset.flag,
      doctors = this.data.doctors,
      doctorInfo = '';

    doctors.forEach(ele => {
      if (ele.doctorId == obj.doctorId) {
        doctorInfo = ele
      }
    })
    console.log(doctorInfo);

    console.log(obj);
    console.log(JSON.stringify(obj));


    if (flag == 'topic') {
      wx.navigateTo({
        url: '/content/pages/topicDetail/topicDetail?doctor=' + JSON.stringify(doctorInfo),
        success: function (res) {
          // 通过eventChannel向被打开页面传送数据
          res.eventChannel.emit('topicDetail', obj)
        }
      })
    } else {
      wx.navigateTo({
        url: '/content/pages/videoDetail/videoDetail?video=' + JSON.stringify(obj) + '&doctor=' + JSON.stringify(doctorInfo),
      })
    }

  },


  getDoctorIndex() {

    var url = app.globalData.url + 'main/listDoctorMain',
      that = this,
      topicList = [],
      videoList = [];
    wx.showLoading({
      title: '',
    })

    app.wxRequest(url, {
        doctorId: app.globalData.doctorId
      }, res => {
        console.log(res);
        var datas = res.data;
        if (res.flag) {
          wx.hideLoading()
          console.log(datas);

          datas.topics.forEach(item => {
            if (item.topicType == 0) {
              topicList.push(item)
            } else if (item.topicType == 1) {
              videoList.push(item)
            }
          })

          that.setData({
            patientNum: datas.patientNum,
            registerNum: datas.registerNum,
            talkNum: datas.talkNum,
            topicList,
            videoList,
          })

          console.log(this.data);


        }
      },
      err => {
        console.log(err);
      })

  },

  getIndex() {
    var url = app.globalData.url + "main/getMain",
      that = this,
      topicList = [],
      videoList = [],
      user = this.data.user,
      myDoctorInfo = '';
    wx.showLoading({
      title: '',
    })

    app.wxRequest(url, {}, res => {
        console.log(res);
        var datas = res.data;
        if (res.flag) {
          wx.hideLoading()
          console.log(datas);
          datas.doctors.forEach(item => {
            item.imgUrl = `/img/department/${item.departmentId||6}.png`
            if (item.doctorId == user.doctorId) {
              myDoctorInfo = item
            }
          })

          datas.topicList.forEach(item => {
            if (item.topicType == 0) {
              topicList.push(item)
            } else if (item.topicType == 1) {
              videoList.push(item)
            }
          })

          that.setData({
            topicList,
            videoList,
            doctors: datas.doctors,
            myDoctorInfo
          })

          console.log(this.data);


        }
      },
      err => {
        console.log(err);
      })
  },

  avatarError(e) {
    console.log(e);
    var index = e.currentTarget.dataset.imgindex,
      list = this.data.doctors,
      src = '/img/userImg.jpg';
    list[index].doctorPic = src
    this.setData({
      doctors: list
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    this.setData({
      baseUrl: app.globalData.path,
      user: app.globalData.user,
    })
    if (this.data.user.userType == 0) {
      this.getIndex()
    } else {
      this.getDoctorIndex()
    }
    console.log(app.globalData);
    console.log(this.data);

  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // var that = this;

    // if (app.globalData.user != '') {
    //   that.setData({
    //     baseUrl: app.globalData.path,
    //     user: app.globalData.user,
    //   })
    //   if (that.data.user.userType == 0) {
    //     that.getIndex()
    //   } else {
    //     that.getDoctorIndex()
    //   }
    //   console.log(app.globalData);
    //   console.log(this.data);
    // } else {
    //   app.CallBack = () => {
    //     that.setData({
    //       baseUrl: app.globalData.path,
    //       user: app.globalData.user,
    //     })
    //     if (that.data.user.userType == 0) {
    //       that.getIndex()
    //     } else {
    //       that.getDoctorIndex()
    //     }
    //     console.log(app.globalData);
    //     console.log(this.data);
    //   }
    // }

    // var timer = setInterval(() => {
    //   if (app.globalData.user != '') {
    //     // app.globalData.user.userType = 0
    //     that.setData({
    //       baseUrl: app.globalData.path,
    //       user: app.globalData.user,
    //     })
    //     if (that.data.user.userType == 0) {
    //       that.getIndex()
    //     } else {
    //       that.getDoctorIndex()
    //     }
    //     console.log(app.globalData);
    //     console.log(this.data);

    //     clearInterval(timer)
    //   }
    // }, 200);


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})